package com.ielts.fragment;

import vn.com.baseapplication.BaseActivity;
import vn.com.baseapplication.BaseFragment;
import android.annotation.SuppressLint;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.ielts.IeltsConstant;
import com.ielts.activity.R;
import com.ielts.model.Guild;
import com.ielts.services.IeltsUrl;

@SuppressLint({ "SetJavaScriptEnabled", "NewApi" })
public class HelpFragment extends BaseFragment {
	BaseActivity mActivity;
	WebView wvHelp;
	View loading;
	
	TextView year;
	ImageView arrow;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mActivity = (BaseActivity) getActivity();

		View layout = inflater.inflate(R.layout.fragment_help, null);
		
		year = (TextView)layout.findViewById(R.id.header_tv_year);
		arrow = (ImageView)layout.findViewById(R.id.header_iv_arrow);
		wvHelp = (WebView) layout.findViewById(R.id.webview_help);
		
		year.setText("Help");
		arrow.setVisibility(View.INVISIBLE);
		
		
		wvHelp.setBackgroundColor(0x00000000);
		if (Build.VERSION.SDK_INT >= 11) wvHelp.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null);
		wvHelp.setWebViewClient(new WebViewClient()
		{
		    @Override
		    public void onPageFinished(WebView view, String url)
		    {
		        view.setBackgroundColor(0x00000000);
		        if (Build.VERSION.SDK_INT >= 11) view.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null);
		    }
		});
		
		loading = layout.findViewById(R.id.loading);

		WebSettings settings = wvHelp.getSettings();
		settings.setJavaScriptEnabled(true);
		wvHelp.loadUrl("file:///android_asset/web/guild.html");
		wvHelp.setLongClickable(false);
		
		
		setLoading(false);
		return layout;
	}

	@Override
	public void onResume() {
		super.onResume();
		getGuild();
	}

	public void getGuild() {
		String urlGuild = IeltsUrl.URL_GUILD;
//		network.get(IeltsConstant.GET_GUILD_FROM_SERVER, this.getActivity(),
//				IeltsUrl.URL_GUILD);
		wvHelp.loadUrl(urlGuild);
	}

	@Override
	public void onNetSuccess(int requestId, final String response) {
		super.onNetSuccess(requestId, response);
		mActivity.onNetSuccess(requestId, response);
		setLoading(false);
		switch (requestId) {
		case IeltsConstant.GET_GUILD_FROM_SERVER:
			network.setCache(IeltsUrl.URL_GUILD, response);
			onNetSuccessCache(requestId, response);
			break;

		default:
			break;
		}
	}

	@Override
	public void onNetSuccessCache(int requestId, final String response) {
		mActivity.onNetSuccessCache(requestId, response);
		switch (requestId) {
		case IeltsConstant.GET_GUILD_FROM_SERVER:
			mActivity.runOnUiThread(new Runnable() {

				@Override
				public void run() {
					try{
					Gson gson = new Gson();
					Guild guild = gson.fromJson(response, Guild.class);
					String helpUrl = guild.dataValue.replaceAll("\\", "");
					wvHelp.loadUrl(helpUrl);
					Log.e(TAG, helpUrl);
					}catch(NullPointerException e){
						e.printStackTrace();
					}catch (JsonSyntaxException e) {
						e.printStackTrace();
					}
				}
			});
			break;

		default:
			break;
		}
	}
	
	@Override
	public void onNetLoading(int requestId) {
		super.onNetLoading(requestId);
		mActivity.onNetLoading(requestId);
		setLoading(true);
	}
	
	@Override
	public void onNetFailed(int requestId, String error) {
		super.onNetFailed(requestId, error);
		setLoading(false);
	}
	
	@Override
	public void onNetTimeOut(int requestId, Throwable error) {
		super.onNetTimeOut(requestId, error);
		setLoading(false);
	}
	
	public void setLoading(final boolean isLoading){
		mActivity.runOnUiThread(new Runnable() {
			
			@Override
			public void run() {
				if(isLoading){
					loading.setVisibility(View.VISIBLE);
				}else{
					loading.setVisibility(View.GONE);
				}
			}
		});
	}
}
